package com.abo.yaas.webservice.client;

import java.util.List;

import com.abo.yaas.domain.Auction;
import com.abo.yaas.domain.Category;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.vaadin.addon.jpacontainer.Filter;
import com.vaadin.addon.jpacontainer.filter.ComparisonFilter;
import com.vaadin.addon.jpacontainer.filter.Conjunction;
import com.vaadin.addon.jpacontainer.filter.StringComparisonFilter;

@SuppressWarnings("serial")
public class AuctionProviderProxy extends EntityProviderProxy<Auction> {
    public AuctionProviderProxy(WebResource resource) {
        super(Auction.class, resource);
    }

    public List<Object> getUnclosedAuctionIds() {
        return null;
    }

    @Override
    protected void prepareFilter(MultivaluedMapImpl params, final Filter filter) {
        /**
         * If filter is complex (both category and title filters are applies)
         * recursively parse them
         */
        if (filter instanceof Conjunction) {
            for (final Filter f : ((Conjunction) filter).getFilters()) {
                prepareFilter(params, f);
            }
        }

        String property = "";
        Object value = null;

        // / Extracting the category filtering criteria if there is one
        if (filter instanceof ComparisonFilter) {
            final ComparisonFilter compFilter = (ComparisonFilter) filter;
            property = compFilter.getPropertyId().toString();
            value = compFilter.getValue();
            if ("category".equals(property) && value instanceof Category) {
                params.putSingle("categoryId", ((Category) value).getId());
            } else if ("state".equals(property)) {
                params.putSingle("state", value);
            }
        }

        // / Extracting the title filtering criteria if there is one
        if (filter instanceof StringComparisonFilter) {
            final StringComparisonFilter strCompFilter = (StringComparisonFilter) filter;
            property = strCompFilter.getPropertyId().toString();
            value = strCompFilter.getValue();
            if ("title".equals(property) && value instanceof String) {
                params.putSingle("title", value);
            }
        }
    }
}
